home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Lib / tkinter / Tkinter.py < prev   
Encoding:
Python Source  |  1996-10-25  |  53.6 KB  |  1,683 lines  |  [TEXT/Pyth]

  1. # Tkinter.py -- Tk/Tcl widget wrappers
  2.  
  3. __version__ = "$Revision: 1.63 $"
  4.  
  5. try:
  6.     # See if modern _tkinter is present
  7.     import _tkinter
  8.     tkinter = _tkinter # b/w compat
  9. except ImportError:
  10.     # No modern _tkinter -- try oldfashioned tkinter
  11.     import tkinter
  12.     if hasattr(tkinter, "__path__"):
  13.         import sys, os
  14.         # Append standard platform specific directory
  15.         p = tkinter.__path__
  16.         for dir in sys.path:
  17.             if (dir not in p and
  18.                 os.path.basename(dir) == sys.platform):
  19.                 p.append(dir)
  20.         del sys, os, p, dir
  21.         from tkinter import tkinter
  22. TclError = tkinter.TclError
  23. from types import *
  24. from Tkconstants import *
  25. import string; _string = string; del string
  26.  
  27. TkVersion = _string.atof(tkinter.TK_VERSION)
  28. TclVersion = _string.atof(tkinter.TCL_VERSION)
  29.  
  30. ######################################################################
  31. # Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
  32. # they are defined here (and not in Tkconstants):
  33. ######################################################################
  34. if TkVersion >= 4.1:
  35.     READABLE = 2
  36.     WRITABLE = 4
  37.     EXCEPTION = 8
  38. else:
  39.     READABLE = 1
  40.     WRITABLE = 2
  41.     EXCEPTION = 4
  42.     
  43.     
  44. def _flatten(tuple):
  45.     res = ()
  46.     for item in tuple:
  47.         if type(item) in (TupleType, ListType):
  48.             res = res + _flatten(item)
  49.         elif item is not None:
  50.             res = res + (item,)
  51.     return res
  52.  
  53. def _cnfmerge(cnfs):
  54.     if type(cnfs) is DictionaryType:
  55.         return cnfs
  56.     elif type(cnfs) in (NoneType, StringType):
  57.         
  58.         return cnfs
  59.     else:
  60.         cnf = {}
  61.         for c in _flatten(cnfs):
  62.             for k, v in c.items():
  63.                 cnf[k] = v
  64.         return cnf
  65.  
  66. class Event:
  67.     pass
  68.  
  69. _default_root = None
  70.  
  71. def _tkerror(err):
  72.     pass
  73.  
  74. def _exit(code='0'):
  75.     raise SystemExit, code
  76.  
  77. _varnum = 0
  78. class Variable:
  79.     _default = ""
  80.     def __init__(self, master=None):
  81.         global _default_root
  82.         global _varnum
  83.         if master:
  84.             self._tk = master.tk
  85.         else:
  86.             self._tk = _default_root.tk
  87.         self._name = 'PY_VAR' + `_varnum`
  88.         _varnum = _varnum + 1
  89.         self.set(self._default)
  90.     def __del__(self):
  91.         self._tk.globalunsetvar(self._name)
  92.     def __str__(self):
  93.         return self._name
  94.     def set(self, value):
  95.         return self._tk.globalsetvar(self._name, value)
  96.  
  97. class StringVar(Variable):
  98.     _default = ""
  99.     def __init__(self, master=None):
  100.         Variable.__init__(self, master)
  101.     def get(self):
  102.         return self._tk.globalgetvar(self._name)
  103.  
  104. class IntVar(Variable):
  105.     _default = "0"
  106.     def __init__(self, master=None):
  107.         Variable.__init__(self, master)
  108.     def get(self):
  109.         return self._tk.getint(self._tk.globalgetvar(self._name))
  110.  
  111. class DoubleVar(Variable):
  112.     _default = "0.0"
  113.     def __init__(self, master=None):
  114.         Variable.__init__(self, master)
  115.     def get(self):
  116.         return self._tk.getdouble(self._tk.globalgetvar(self._name))
  117.  
  118. class BooleanVar(Variable):
  119.     _default = "false"
  120.     def __init__(self, master=None):
  121.         Variable.__init__(self, master)
  122.     def get(self):
  123.         return self._tk.getboolean(self._tk.globalgetvar(self._name))
  124.  
  125. def mainloop(n=0):
  126.     _default_root.tk.mainloop(n)
  127.  
  128. def getint(s):
  129.     return _default_root.tk.getint(s)
  130.  
  131. def getdouble(s):
  132.     return _default_root.tk.getdouble(s)
  133.  
  134. def getboolean(s):
  135.     return _default_root.tk.getboolean(s)
  136.  
  137. class Misc:
  138.     def tk_strictMotif(self, boolean=None):
  139.         return self.tk.getboolean(self.tk.call(
  140.             'set', 'tk_strictMotif', boolean))
  141.     def tk_bisque(self):
  142.         self.tk.call('tk_bisque')
  143.     def tk_setPalette(self, *args, **kw):
  144.         apply(self.tk.call, ('tk_setPalette',)
  145.               + _flatten(args) + _flatten(kw.items()))
  146.     def tk_menuBar(self, *args):
  147.         pass # obsolete since Tk 4.0
  148.     def wait_variable(self, name='PY_VAR'):
  149.         self.tk.call('tkwait', 'variable', name)
  150.     waitvar = wait_variable # XXX b/w compat
  151.     def wait_window(self, window=None):
  152.         if window == None:
  153.             window = self
  154.         self.tk.call('tkwait', 'window', window._w)
  155.     def wait_visibility(self, window=None):
  156.         if window == None:
  157.             window = self
  158.         self.tk.call('tkwait', 'visibility', window._w)
  159.     def setvar(self, name='PY_VAR', value='1'):
  160.         self.tk.setvar(name, value)
  161.     def getvar(self, name='PY_VAR'):
  162.         return self.tk.getvar(name)
  163.     def getint(self, s):
  164.         return self.tk.getint(s)
  165.     def getdouble(self, s):
  166.         return self.tk.getdouble(s)
  167.     def getboolean(self, s):
  168.         return self.tk.getboolean(s)
  169.     def focus_set(self):
  170.         self.tk.call('focus', self._w)
  171.     focus = focus_set # XXX b/w compat?
  172.     def focus_force(self):
  173.         self.tk.call('focus', '-force', self._w)
  174.     def focus_get(self):
  175.         name = self.tk.call('focus')
  176.         if name == 'none' or not name: return None
  177.         return self._nametowidget(name)
  178.     def focus_displayof(self):
  179.         name = self.tk.call('focus', '-displayof', self._w)
  180.         if name == 'none' or not name: return None
  181.         return self._nametowidget(name)
  182.     def focus_lastfor(self):
  183.         name = self.tk.call('focus', '-lastfor', self._w)
  184.         if name == 'none' or not name: return None
  185.         return self._nametowidget(name)
  186.     def tk_focusFollowsMouse(self):
  187.         self.tk.call('tk_focusFollowsMouse')
  188.     def tk_focusNext(self):
  189.         name = self.tk.call('tk_focusNext', self._w)
  190.         if not name: return None
  191.         return self._nametowidget(name)
  192.     def tk_focusPrev(self):
  193.         name = self.tk.call('tk_focusPrev', self._w)
  194.         if not name: return None
  195.         return self._nametowidget(name)
  196.     def after(self, ms, func=None, *args):
  197.         if not func:
  198.             # I'd rather use time.sleep(ms*0.001)
  199.             self.tk.call('after', ms)
  200.         else:
  201.             # XXX Disgusting hack to clean up after calling func
  202.             tmp = []
  203.             def callit(func=func, args=args, tk=self.tk, tmp=tmp):
  204.                 try:
  205.                     apply(func, args)
  206.                 finally:
  207.                     tk.deletecommand(tmp[0])
  208.             name = self._register(callit)
  209.             tmp.append(name)
  210.             return self.tk.call('after', ms, name)
  211.     def after_idle(self, func, *args):
  212.         return apply(self.after, ('idle', func) + args)
  213.     def after_cancel(self, id):
  214.         self.tk.call('after', 'cancel', id)
  215.     def bell(self, displayof=0):
  216.         apply(self.tk.call, ('bell',) + self._displayof(displayof))
  217.     # Clipboard handling:
  218.     def clipboard_clear(self, **kw):
  219.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  220.         apply(self.tk.call,
  221.               ('clipboard', 'clear') + self._options(kw))
  222.     def clipboard_append(self, string, **kw):
  223.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  224.         apply(self.tk.call,
  225.               ('clipboard', 'append') + self._options(kw)
  226.               + ('--', string))
  227.     # XXX grab current w/o window argument
  228.     def grab_current(self):
  229.         name = self.tk.call('grab', 'current', self._w)
  230.         if not name: return None
  231.         return self._nametowidget(name)
  232.     def grab_release(self):
  233.         self.tk.call('grab', 'release', self._w)
  234.     def grab_set(self):
  235.         self.tk.call('grab', 'set', self._w)
  236.     def grab_set_global(self):
  237.         self.tk.call('grab', 'set', '-global', self._w)
  238.     def grab_status(self):
  239.         status = self.tk.call('grab', 'status', self._w)
  240.         if status == 'none': status = None
  241.         return status
  242.     def lower(self, belowThis=None):
  243.         self.tk.call('lower', self._w, belowThis)
  244.     def option_add(self, pattern, value, priority = None):
  245.         self.tk.call('option', 'add', pattern, value, priority)
  246.     def option_clear(self):
  247.         self.tk.call('option', 'clear')
  248.     def option_get(self, name, className):
  249.         return self.tk.call('option', 'get', self._w, name, className)
  250.     def option_readfile(self, fileName, priority = None):
  251.         self.tk.call('option', 'readfile', fileName, priority)
  252.     def selection_clear(self, **kw):
  253.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  254.         apply(self.tk.call, ('selection', 'clear') + self._options(kw))
  255.     def selection_get(self, **kw):
  256.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  257.         return apply(self.tk.call,
  258.                  ('selection', 'get') + self._options(kw))
  259.     def selection_handle(self, command, **kw):
  260.         name = self._register(command)
  261.         apply(self.tk.call,
  262.               ('selection', 'handle') + self._options(kw)
  263.               + (self._w, name))
  264.     def selection_own(self, **kw):
  265.         "Become owner of X selection."
  266.         apply(self.tk.call,
  267.               ('selection', 'own') + self._options(kw) + (self._w,))
  268.     def selection_own_get(self, **kw):
  269.         "Find owner of X selection."
  270.         if not kw.has_key('displayof'): kw['displayof'] = self._w
  271.         return self._nametowidget(
  272.             apply(self.tk.call,
  273.                   ('selection', 'own') + self._options(kw)))
  274.     def send(self, interp, cmd, *args):
  275.         return apply(self.tk.call, ('send', interp, cmd) + args)
  276.     def lower(self, belowThis=None):
  277.         self.tk.call('lower', self._w, belowThis)
  278.     def tkraise(self, aboveThis=None):
  279.         self.tk.call('raise', self._w, aboveThis)
  280.     lift = tkraise
  281.     def colormodel(self, value=None):
  282.         return self.tk.call('tk', 'colormodel', self._w, value)
  283.     def winfo_atom(self, name, displayof=0):
  284.         args = ('winfo', 'atom') + self._displayof(displayof) + (name,)
  285.         return self.tk.getint(apply(self.tk.call, args))
  286.     def winfo_atomname(self, id, displayof=0):
  287.         args = ('winfo', 'atomname') \
  288.                + self._displayof(displayof) + (id,)
  289.         return apply(self.tk.call, args)
  290.     def winfo_cells(self):
  291.         return self.tk.getint(
  292.             self.tk.call('winfo', 'cells', self._w))
  293.     def winfo_children(self):
  294.         return map(self._nametowidget,
  295.                self.tk.splitlist(self.tk.call(
  296.                    'winfo', 'children', self._w)))
  297.     def winfo_class(self):
  298.         return self.tk.call('winfo', 'class', self._w)
  299.     def winfo_colormapfull(self):
  300.         return self.tk.getboolean(
  301.             self.tk.call('winfo', 'colormapfull'))
  302.     def winfo_containing(self, rootX, rootY, displayof=0):
  303.         args = ('winfo', 'containing') \
  304.                + self._displayof(displayof) + (rootX, rootY)
  305.         return self._nametowidget(apply(self.tk.call, args))
  306.     def winfo_depth(self):
  307.         return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
  308.     def winfo_exists(self):
  309.         return self.tk.getint(
  310.             self.tk.call('winfo', 'exists', self._w))
  311.     def winfo_fpixels(self, number):
  312.         return self.tk.getdouble(self.tk.call(
  313.             'winfo', 'fpixels', self._w, number))
  314.     def winfo_geometry(self):
  315.         return self.tk.call('winfo', 'geometry', self._w)
  316.     def winfo_height(self):
  317.         return self.tk.getint(
  318.             self.tk.call('winfo', 'height', self._w))
  319.     def winfo_id(self):
  320.         return self.tk.getint(
  321.             self.tk.call('winfo', 'id', self._w))
  322.     def winfo_interps(self, displayof=0):
  323.         args = ('winfo', 'interps') + self._displayof(displayof)
  324.         return self.tk.splitlist(apply(self.tk.call, args))
  325.     def winfo_ismapped(self):
  326.         return self.tk.getint(
  327.             self.tk.call('winfo', 'ismapped', self._w))
  328.     def winfo_name(self):
  329.         return self.tk.call('winfo', 'name', self._w)
  330.     def winfo_parent(self):
  331.         return self.tk.call('winfo', 'parent', self._w)
  332.     def winfo_pathname(self, id, displayof=0):
  333.         args = ('winfo', 'pathname') \
  334.                + self._displayof(displayof) + (id,)
  335.         return apply(self.tk.call, args)
  336.     def winfo_pixels(self, number):
  337.         return self.tk.getint(
  338.             self.tk.call('winfo', 'pixels', self._w, number))
  339.     def winfo_reqheight(self):
  340.         return self.tk.getint(
  341.             self.tk.call('winfo', 'reqheight', self._w))
  342.     def winfo_reqwidth(self):
  343.         return self.tk.getint(
  344.             self.tk.call('winfo', 'reqwidth', self._w))
  345.     def winfo_rgb(self, color):
  346.         return self._getints(
  347.             self.tk.call('winfo', 'rgb', self._w, color))
  348.     def winfo_rootx(self):
  349.         return self.tk.getint(
  350.             self.tk.call('winfo', 'rootx', self._w))
  351.     def winfo_rooty(self):
  352.         return self.tk.getint(
  353.             self.tk.call('winfo', 'rooty', self._w))
  354.     def winfo_screen(self):
  355.         return self.tk.call('winfo', 'screen', self._w)
  356.     def winfo_screencells(self):
  357.         return self.tk.getint(
  358.             self.tk.call('winfo', 'screencells', self._w))
  359.     def winfo_screendepth(self):
  360.         return self.tk.getint(
  361.             self.tk.call('winfo', 'screendepth', self._w))
  362.     def winfo_screenheight(self):
  363.         return self.tk.getint(
  364.             self.tk.call('winfo', 'screenheight', self._w))
  365.     def winfo_screenmmheight(self):
  366.         return self.tk.getint(
  367.             self.tk.call('winfo', 'screenmmheight', self._w))
  368.     def winfo_screenmmwidth(self):
  369.         return self.tk.getint(
  370.             self.tk.call('winfo', 'screenmmwidth', self._w))
  371.     def winfo_screenvisual(self):
  372.         return self.tk.call('winfo', 'screenvisual', self._w)
  373.     def winfo_screenwidth(self):
  374.         return self.tk.getint(
  375.             self.tk.call('winfo', 'screenwidth', self._w))
  376.     def winfo_toplevel(self):
  377.         return self._nametowidget(self.tk.call(
  378.             'winfo', 'toplevel', self._w))
  379.     def winfo_visual(self):
  380.         return self.tk.call('winfo', 'visual', self._w)
  381.     def winfo_vrootheight(self):
  382.         return self.tk.getint(
  383.             self.tk.call('winfo', 'vrootheight', self._w))
  384.     def winfo_vrootwidth(self):
  385.         return self.tk.getint(
  386.             self.tk.call('winfo', 'vrootwidth', self._w))
  387.     def winfo_vrootx(self):
  388.         return self.tk.getint(
  389.             self.tk.call('winfo', 'vrootx', self._w))
  390.     def winfo_vrooty(self):
  391.         return self.tk.getint(
  392.             self.tk.call('winfo', 'vrooty', self._w))
  393.     def winfo_width(self):
  394.         return self.tk.getint(
  395.             self.tk.call('winfo', 'width', self._w))
  396.     def winfo_x(self):
  397.         return self.tk.getint(
  398.             self.tk.call('winfo', 'x', self._w))
  399.     def winfo_y(self):
  400.         return self.tk.getint(
  401.             self.tk.call('winfo', 'y', self._w))
  402.     def update(self):
  403.         self.tk.call('update')
  404.     def update_idletasks(self):
  405.         self.tk.call('update', 'idletasks')
  406.     def bindtags(self, tagList=None):
  407.         if tagList is None:
  408.             return self.tk.splitlist(
  409.                 self.tk.call('bindtags', self._w))
  410.         else:
  411.             self.tk.call('bindtags', self._w, tagList)
  412.     def _bind(self, what, sequence, func, add):
  413.         if func:
  414.             cmd = ("%sset _tkinter_break [%s %s]\n"
  415.                    'if {"$_tkinter_break" == "break"} break\n') \
  416.                    % (add and '+' or '',
  417.                   self._register(func, self._substitute),
  418.                   _string.join(self._subst_format))
  419.             apply(self.tk.call, what + (sequence, cmd))
  420.         elif func == '':
  421.             apply(self.tk.call, what + (sequence, func))
  422.         else:
  423.             return apply(self.tk.call, what + (sequence,))
  424.     def bind(self, sequence=None, func=None, add=None):
  425.         return self._bind(('bind', self._w), sequence, func, add)
  426.     def unbind(self, sequence):
  427.         self.tk.call('bind', self._w, sequence, '')
  428.     def bind_all(self, sequence=None, func=None, add=None):
  429.         return self._bind(('bind', 'all'), sequence, func, add)
  430.     def unbind_all(self, sequence):
  431.         self.tk.call('bind', 'all' , sequence, '')
  432.     def bind_class(self, className, sequence=None, func=None, add=None):
  433.         self._bind(('bind', className), sequence, func, add)
  434.     def unbind_class(self, className, sequence):
  435.         self.tk.call('bind', className , sequence, '')
  436.     def mainloop(self, n=0):
  437.         self.tk.mainloop(n)
  438.     def quit(self):
  439.         self.tk.quit()
  440.     def _getints(self, string):
  441.         if not string: return None
  442.         return tuple(map(self.tk.getint, self.tk.splitlist(string)))
  443.     def _getdoubles(self, string):
  444.         if not string: return None
  445.         return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
  446.     def _getboolean(self, string):
  447.         if string:
  448.             return self.tk.getboolean(string)
  449.     def _displayof(self, displayof):
  450.         if displayof:
  451.             return ('-displayof', displayof)
  452.         if displayof is None:
  453.             return ('-displayof', self._w)
  454.         return ()
  455.     def _options(self, cnf, kw = None):
  456.         if kw:
  457.             cnf = _cnfmerge((cnf, kw))
  458.         else:
  459.             cnf = _cnfmerge(cnf)
  460.         res = ()
  461.         for k, v in cnf.items():
  462.             if v is not None:
  463.                 if k[-1] == '_': k = k[:-1]
  464.                 if callable(v):
  465.                     v = self._register(v)
  466.                 res = res + ('-'+k, v)
  467.         return res
  468.     def _nametowidget(self, name):
  469.         w = self
  470.         if name[0] == '.':
  471.             w = w._root()
  472.             name = name[1:]
  473.         find = _string.find
  474.         while name:
  475.             i = find(name, '.')
  476.             if i >= 0:
  477.                 name, tail = name[:i], name[i+1:]
  478.             else:
  479.                 tail = ''
  480.             w = w.children[name]
  481.             name = tail
  482.         return w
  483.     def _register(self, func, subst=None):
  484.         f = CallWrapper(func, subst, self).__call__
  485.         name = `id(f)`
  486.         try:
  487.             func = func.im_func
  488.         except AttributeError:
  489.             pass
  490.         try:
  491.             name = name + func.__name__
  492.         except AttributeError:
  493.             pass
  494.         self.tk.createcommand(name, f)
  495.         return name
  496.     register = _register
  497.     def _root(self):
  498.         w = self
  499.         while w.master: w = w.master
  500.         return w
  501.     _subst_format = ('%#', '%b', '%f', '%h', '%k', 
  502.              '%s', '%t', '%w', '%x', '%y',
  503.              '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
  504.     def _substitute(self, *args):
  505.         tk = self.tk
  506.         if len(args) != len(self._subst_format): return args
  507.         nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
  508.         # Missing: (a, c, d, m, o, v, B, R)
  509.         e = Event()
  510.         e.serial = tk.getint(nsign)
  511.         e.num = tk.getint(b)
  512.         try: e.focus = tk.getboolean(f)
  513.         except TclError: pass
  514.         e.height = tk.getint(h)
  515.         e.keycode = tk.getint(k)
  516.         # For Visibility events, event state is a string and
  517.         # not an integer:
  518.         try:
  519.             e.state = tk.getint(s)
  520.         except TclError:
  521.             e.state = s
  522.         e.time = tk.getint(t)
  523.         e.width = tk.getint(w)
  524.         e.x = tk.getint(x)
  525.         e.y = tk.getint(y)
  526.         e.char = A
  527.         try: e.send_event = tk.getboolean(E)
  528.         except TclError: pass
  529.         e.keysym = K
  530.         e.keysym_num = tk.getint(N)
  531.         e.type = T
  532.         e.widget = self._nametowidget(W)
  533.         e.x_root = tk.getint(X)
  534.         e.y_root = tk.getint(Y)
  535.         return (e,)
  536.     def _report_exception(self):
  537.         import sys
  538.         exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
  539.         root = self._root()
  540.         root.report_callback_exception(exc, val, tb)
  541.  
  542. class CallWrapper:
  543.     def __init__(self, func, subst, widget):
  544.         self.func = func
  545.         self.subst = subst
  546.         self.widget = widget
  547.     def __call__(self, *args):
  548.         try:
  549.             if self.subst:
  550.                 args = apply(self.subst, args)
  551.             return apply(self.func, args)
  552.         except SystemExit, msg:
  553.             raise SystemExit, msg
  554.         except:
  555.             self.widget._report_exception()
  556.  
  557. class Wm:
  558.     def aspect(self, 
  559.            minNumer=None, minDenom=None, 
  560.            maxNumer=None, maxDenom=None):
  561.         return self._getints(
  562.             self.tk.call('wm', 'aspect', self._w, 
  563.                      minNumer, minDenom, 
  564.                      maxNumer, maxDenom))
  565.     def client(self, name=None):
  566.         return self.tk.call('wm', 'client', self._w, name)
  567.     def colormapwindows(self, *wlist):
  568.         args = ('wm', 'colormapwindows', self._w) + _flatten(wlist)
  569.         return map(self._nametowidget, apply(self.tk.call, args))
  570.     def command(self, value=None):
  571.         return self.tk.call('wm', 'command', self._w, value)
  572.     def deiconify(self):
  573.         return self.tk.call('wm', 'deiconify', self._w)
  574.     def focusmodel(self, model=None):
  575.         return self.tk.call('wm', 'focusmodel', self._w, model)
  576.     def frame(self):
  577.         return self.tk.call('wm', 'frame', self._w)
  578.     def geometry(self, newGeometry=None):
  579.         return self.tk.call('wm', 'geometry', self._w, newGeometry)
  580.     def grid(self,
  581.          baseWidht=None, baseHeight=None, 
  582.          widthInc=None, heightInc=None):
  583.         return self._getints(self.tk.call(
  584.             'wm', 'grid', self._w,
  585.             baseWidht, baseHeight, widthInc, heightInc))
  586.     def group(self, pathName=None):
  587.         return self.tk.call('wm', 'group', self._w, pathName)
  588.     def iconbitmap(self, bitmap=None):
  589.         return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
  590.     def iconify(self):
  591.         return self.tk.call('wm', 'iconify', self._w)
  592.     def iconmask(self, bitmap=None):
  593.         return self.tk.call('wm', 'iconmask', self._w, bitmap)
  594.     def iconname(self, newName=None):
  595.         return self.tk.call('wm', 'iconname', self._w, newName)
  596.     def iconposition(self, x=None, y=None):
  597.         return self._getints(self.tk.call(
  598.             'wm', 'iconposition', self._w, x, y))
  599.     def iconwindow(self, pathName=None):
  600.         return self.tk.call('wm', 'iconwindow', self._w, pathName)
  601.     def maxsize(self, width=None, height=None):
  602.         return self._getints(self.tk.call(
  603.             'wm', 'maxsize', self._w, width, height))
  604.     def minsize(self, width=None, height=None):
  605.         return self._getints(self.tk.call(
  606.             'wm', 'minsize', self._w, width, height))
  607.     def overrideredirect(self, boolean=None):
  608.         return self._getboolean(self.tk.call(
  609.             'wm', 'overrideredirect', self._w, boolean))
  610.     def positionfrom(self, who=None):
  611.         return self.tk.call('wm', 'positionfrom', self._w, who)
  612.     def protocol(self, name=None, func=None):
  613.             if callable(func):
  614.             command = self._register(func)
  615.         else:
  616.             command = func
  617.         return self.tk.call(
  618.             'wm', 'protocol', self._w, name, command)
  619.     def resizable(self, width=None, height=None):
  620.         return self.tk.call('wm', 'resizable', self._w, width, height)
  621.     def sizefrom(self, who=None):
  622.         return self.tk.call('wm', 'sizefrom', self._w, who)
  623.     def state(self):
  624.         return self.tk.call('wm', 'state', self._w)
  625.     def title(self, string=None):
  626.         return self.tk.call('wm', 'title', self._w, string)
  627.     def transient(self, master=None):
  628.         return self.tk.call('wm', 'transient', self._w, master)
  629.     def withdraw(self):
  630.         return self.tk.call('wm', 'withdraw', self._w)
  631.  
  632. class Tk(Misc, Wm):
  633.     _w = '.'
  634.     def __init__(self, screenName=None, baseName=None, className='Tk'):
  635.         global _default_root
  636.         self.master = None
  637.         self.children = {}
  638.         if baseName is None:
  639.             import sys, os
  640.             baseName = os.path.basename(sys.argv[0])
  641.             baseName, ext = os.path.splitext(baseName)
  642.             if ext not in ('.py', 'pyc'): baseName = baseName + ext
  643.         self.tk = tkinter.create(screenName, baseName, className)
  644.         try:
  645.             # Disable event scanning except for Command-Period
  646.             import MacOS
  647.             MacOS.EnableAppswitch(0)
  648.         except ImportError:
  649.             pass
  650.         else:
  651.             # Work around nasty MacTk bug
  652.             self.update()
  653.         # Version sanity checks
  654.         tk_version = self.tk.getvar('tk_version')
  655.         if tk_version != tkinter.TK_VERSION:
  656.             raise RuntimeError, \
  657.             "tk.h version (%s) doesn't match libtk.a version (%s)" \
  658.             % (tkinter.TK_VERSION, tk_version)
  659.         tcl_version = self.tk.getvar('tcl_version')
  660.         if tcl_version != tkinter.TCL_VERSION:
  661.             raise RuntimeError, \
  662.             "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
  663.             % (tkinter.TCL_VERSION, tcl_version)
  664.         if TkVersion < 4.0:
  665.             raise RuntimeError, \
  666.             "Tk 4.0 or higher is required; found Tk %s" \
  667.             % str(TkVersion)
  668.         self.tk.createcommand('tkerror', _tkerror)
  669.         self.tk.createcommand('exit', _exit)
  670.         self.readprofile(baseName, className)
  671.         if not _default_root:
  672.             _default_root = self
  673.     def destroy(self):
  674.         for c in self.children.values(): c.destroy()
  675.         self.tk.call('destroy', self._w)
  676.     def __str__(self):
  677.         return self._w
  678.     def readprofile(self, baseName, className):
  679.         import os
  680.         if os.environ.has_key('HOME'): home = os.environ['HOME']
  681.         else: home = os.curdir
  682.         class_tcl = os.path.join(home, '.%s.tcl' % className)
  683.         class_py = os.path.join(home, '.%s.py' % className)
  684.         base_tcl = os.path.join(home, '.%s.tcl' % baseName)
  685.         base_py = os.path.join(home, '.%s.py' % baseName)
  686.         dir = {'self': self}
  687.         exec 'from Tkinter import *' in dir
  688.         if os.path.isfile(class_tcl):
  689.             print 'source', `class_tcl`
  690.             self.tk.call('source', class_tcl)
  691.         if os.path.isfile(class_py):
  692.             print 'execfile', `class_py`
  693.             execfile(class_py, dir)
  694.         if os.path.isfile(base_tcl):
  695.             print 'source', `base_tcl`
  696.             self.tk.call('source', base_tcl)
  697.         if os.path.isfile(base_py):
  698.             print 'execfile', `base_py`
  699.             execfile(base_py, dir)
  700.     def report_callback_exception(self, exc, val, tb):
  701.         import traceback
  702.         print "Exception in Tkinter callback"
  703.         traceback.print_exception(exc, val, tb)
  704.  
  705. class Pack:
  706.     def config(self, cnf={}, **kw):
  707.         apply(self.tk.call, 
  708.               ('pack', 'configure', self._w) 
  709.               + self._options(cnf, kw))
  710.     configure = config
  711.     pack = config
  712.     def __setitem__(self, key, value):
  713.         Pack.config({key: value})
  714.     def forget(self):
  715.         self.tk.call('pack', 'forget', self._w)
  716.     pack_forget = forget
  717.     def info(self):
  718.         words = self.tk.splitlist(
  719.             self.tk.call('pack', 'info', self._w))
  720.         dict = {}
  721.         for i in range(0, len(words), 2):
  722.             key = words[i][1:]
  723.             value = words[i+1]
  724.             if value[:1] == '.':
  725.                 value = self._nametowidget(value)
  726.             dict[key] = value
  727.         return dict
  728.     pack_info = info
  729.     _noarg_ = ['_noarg_']
  730.     def propagate(self, flag=_noarg_):
  731.         if flag is Pack._noarg_:
  732.             return self._getboolean(self.tk.call(
  733.                 'pack', 'propagate', self._w))
  734.         else:
  735.             self.tk.call('pack', 'propagate', self._w, flag)
  736.     pack_propagate = propagate
  737.     def slaves(self):
  738.         return map(self._nametowidget,
  739.                self.tk.splitlist(
  740.                    self.tk.call('pack', 'slaves', self._w)))
  741.     pack_slaves = slaves
  742.  
  743. class Place:
  744.     def config(self, cnf={}, **kw):
  745.         for k in ['in_']:
  746.             if kw.has_key(k):
  747.                 kw[k[:-1]] = kw[k]
  748.                 del kw[k]
  749.         apply(self.tk.call, 
  750.               ('place', 'configure', self._w) 
  751.               + self._options(cnf, kw))
  752.     configure = config
  753.     place = config
  754.     def __setitem__(self, key, value):
  755.         Place.config({key: value})
  756.     def forget(self):
  757.         self.tk.call('place', 'forget', self._w)
  758.     place_forget = forget
  759.     def info(self):
  760.         words = self.tk.splitlist(
  761.             self.tk.call('place', 'info', self._w))
  762.         dict = {}
  763.         for i in range(0, len(words), 2):
  764.             key = words[i][1:]
  765.             value = words[i+1]
  766.             if value[:1] == '.':
  767.                 value = self._nametowidget(value)
  768.             dict[key] = value
  769.         return dict
  770.     place_info = info
  771.     def slaves(self):
  772.         return map(self._nametowidget,
  773.                self.tk.splitlist(
  774.                    self.tk.call(
  775.                        'place', 'slaves', self._w)))
  776.     place_slaves = slaves
  777.  
  778. class Grid:
  779.     # Thanks to Masazumi Yoshikawa (yosikawa@isi.edu)
  780.     def config(self, cnf={}, **kw):
  781.         apply(self.tk.call, 
  782.               ('grid', 'configure', self._w) 
  783.               + self._options(cnf, kw))
  784.     grid = config
  785.     def __setitem__(self, key, value):
  786.         Grid.config({key: value})
  787.     def bbox(self, column, row):
  788.         return self._getints(
  789.             self.tk.call(
  790.                 'grid', 'bbox', self._w, column, row)) or None
  791.     grid_bbox = bbox
  792.     def columnconfigure(self, index, cnf={}, **kw):
  793.         if type(cnf) is not DictionaryType and not kw:
  794.             options = self._options({cnf: None})
  795.         else:
  796.             options = self._options(cnf, kw)
  797.         res = apply(self.tk.call, 
  798.                   ('grid', 'columnconfigure', self._w, index) 
  799.                   + options)
  800.         if options == ('-minsize', None):
  801.             return self.tk.getint(res) or None
  802.         elif options == ('-weight', None):
  803.             return self.tk.getdouble(res) or None
  804.     def forget(self):
  805.         self.tk.call('grid', 'forget', self._w)
  806.     grid_forget = forget
  807.     def info(self):
  808.         words = self.tk.splitlist(
  809.             self.tk.call('grid', 'info', self._w))
  810.         dict = {}
  811.         for i in range(0, len(words), 2):
  812.             key = words[i][1:]
  813.             value = words[i+1]
  814.             if value[:1] == '.':
  815.                 value = self._nametowidget(value)
  816.             dict[key] = value
  817.         return dict
  818.     grid_info = info
  819.     def location(self, x, y):
  820.         return self._getints(
  821.             self.tk.call(
  822.                 'grid', 'location', self._w, x, y)) or None
  823.     _noarg_ = ['_noarg_']
  824.     def propagate(self, flag=_noarg_):
  825.         if flag is Grid._noarg_:
  826.             return self._getboolean(self.tk.call(
  827.                 'grid', 'propagate', self._w))
  828.         else:
  829.             self.tk.call('grid', 'propagate', self._w, flag)
  830.     grid_propagate = propagate
  831.     def rowconfigure(self, index, cnf={}, **kw):
  832.         if type(cnf) is not DictionaryType and not kw:
  833.             options = self._options({cnf: None})
  834.         else:
  835.             options = self._options(cnf, kw)
  836.         res = apply(self.tk.call, 
  837.                   ('grid', 'rowconfigure', self._w, index) 
  838.                   + options)
  839.         if options == ('-minsize', None):
  840.             return self.tk.getint(res) or None
  841.         elif options == ('-weight', None):
  842.             return self.tk.getdouble(res) or None
  843.     def size(self):
  844.         return self._getints(
  845.             self.tk.call('grid', 'size', self._w)) or None
  846.     def slaves(self, *args):
  847.         return map(self._nametowidget,
  848.                self.tk.splitlist(
  849.                    apply(self.tk.call,
  850.                      ('grid', 'slaves', self._w) + args)))
  851.     grid_slaves = slaves
  852.  
  853. class Widget(Misc, Pack, Place, Grid):
  854.     def _setup(self, master, cnf):
  855.         global _default_root
  856.         if not master:
  857.             if not _default_root:
  858.                 _default_root = Tk()
  859.             master = _default_root
  860.         if not _default_root:
  861.             _default_root = master
  862.         self.master = master
  863.         self.tk = master.tk
  864.         if cnf.has_key('name'):
  865.             name = cnf['name']
  866.             del cnf['name']
  867.         else:
  868.             name = `id(self)`
  869.         self._name = name
  870.         if master._w=='.':
  871.             self._w = '.' + name
  872.         else:
  873.             self._w = master._w + '.' + name
  874.         self.children = {}
  875.         if self.master.children.has_key(self._name):
  876.             self.master.children[self._name].destroy()
  877.         self.master.children[self._name] = self
  878.     def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
  879.         if kw:
  880.             cnf = _cnfmerge((cnf, kw))
  881.         self.widgetName = widgetName
  882.         Widget._setup(self, master, cnf)
  883.         classes = []
  884.         for k in cnf.keys():
  885.             if type(k) is ClassType:
  886.                 classes.append((k, cnf[k]))
  887.                 del cnf[k]
  888.         apply(self.tk.call,
  889.               (widgetName, self._w) + extra + self._options(cnf))
  890.         for k, v in classes:
  891.             k.config(self, v)
  892.     def config(self, cnf=None, **kw):
  893.         # XXX ought to generalize this so tag_config etc. can use it
  894.         if kw:
  895.             cnf = _cnfmerge((cnf, kw))
  896.         elif cnf:
  897.             cnf = _cnfmerge(cnf)
  898.         if cnf is None:
  899.             cnf = {}
  900.             for x in self.tk.split(
  901.                 self.tk.call(self._w, 'configure')):
  902.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  903.             return cnf
  904.         if type(cnf) is StringType:
  905.             x = self.tk.split(self.tk.call(
  906.                 self._w, 'configure', '-'+cnf))
  907.             return (x[0][1:],) + x[1:]
  908.         apply(self.tk.call, (self._w, 'configure')
  909.               + self._options(cnf))
  910.     configure = config
  911.     def cget(self, key):
  912.         return self.tk.call(self._w, 'cget', '-' + key)
  913.     __getitem__ = cget
  914.     def __setitem__(self, key, value):
  915.         Widget.config(self, {key: value})
  916.     def keys(self):
  917.         return map(lambda x: x[0][1:],
  918.                self.tk.split(self.tk.call(self._w, 'configure')))
  919.     def __str__(self):
  920.         return self._w
  921.     def destroy(self):
  922.         for c in self.children.values(): c.destroy()
  923.         if self.master.children.has_key(self._name):
  924.             del self.master.children[self._name]
  925.         self.tk.call('destroy', self._w)
  926.     def _do(self, name, args=()):
  927.         return apply(self.tk.call, (self._w, name) + args)
  928.  
  929. class Toplevel(Widget, Wm):
  930.     def __init__(self, master=None, cnf={}, **kw):
  931.         if kw:
  932.             cnf = _cnfmerge((cnf, kw))
  933.         extra = ()
  934.         for wmkey in ['screen', 'class_', 'class', 'visual',
  935.                   'colormap']:
  936.             if cnf.has_key(wmkey):
  937.                 val = cnf[wmkey]
  938.                 # TBD: a hack needed because some keys
  939.                 # are not valid as keyword arguments
  940.                 if wmkey[-1] == '_': opt = '-'+wmkey[:-1]
  941.                 else: opt = '-'+wmkey
  942.                 extra = extra + (opt, val)
  943.                 del cnf[wmkey]
  944.         Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
  945.         root = self._root()
  946.         self.iconname(root.iconname())
  947.         self.title(root.title())
  948.  
  949. class Button(Widget):
  950.     def __init__(self, master=None, cnf={}, **kw):
  951.         Widget.__init__(self, master, 'button', cnf, kw)
  952.     def tkButtonEnter(self, *dummy):
  953.         self.tk.call('tkButtonEnter', self._w)
  954.     def tkButtonLeave(self, *dummy):
  955.         self.tk.call('tkButtonLeave', self._w)
  956.     def tkButtonDown(self, *dummy):
  957.         self.tk.call('tkButtonDown', self._w)
  958.     def tkButtonUp(self, *dummy):
  959.         self.tk.call('tkButtonUp', self._w)
  960.     def tkButtonInvoke(self, *dummy):
  961.         self.tk.call('tkButtonInvoke', self._w)
  962.     def flash(self):
  963.         self.tk.call(self._w, 'flash')
  964.     def invoke(self):
  965.         self.tk.call(self._w, 'invoke')
  966.  
  967. # Indices:
  968. # XXX I don't like these -- take them away
  969. def AtEnd():
  970.     return 'end'
  971. def AtInsert(*args):
  972.     s = 'insert'
  973.     for a in args:
  974.         if a: s = s + (' ' + a)
  975.     return s
  976. def AtSelFirst():
  977.     return 'sel.first'
  978. def AtSelLast():
  979.     return 'sel.last'
  980. def At(x, y=None):
  981.     if y is None:
  982.         return '@' + `x`        
  983.     else:
  984.         return '@' + `x` + ',' + `y`
  985.  
  986. class Canvas(Widget):
  987.     def __init__(self, master=None, cnf={}, **kw):
  988.         Widget.__init__(self, master, 'canvas', cnf, kw)
  989.     def addtag(self, *args):
  990.         self._do('addtag', args)
  991.     def addtag_above(self, newtag, tagOrId):
  992.         self.addtag(newtag, 'above', tagOrId)
  993.     def addtag_all(self, newtag):
  994.         self.addtag(newtag, 'all')
  995.     def addtag_below(self, newtag, tagOrId):
  996.         self.addtag(newtag, 'below', tagOrId)
  997.     def addtag_closest(self, newtag, x, y, halo=None, start=None):
  998.         self.addtag(newtag, 'closest', x, y, halo, start)
  999.     def addtag_enclosed(self, newtag, x1, y1, x2, y2):
  1000.         self.addtag(newtag, 'enclosed', x1, y1, x2, y2)
  1001.     def addtag_overlapping(self, newtag, x1, y1, x2, y2):
  1002.         self.addtag(newtag, 'overlapping', x1, y1, x2, y2)
  1003.     def addtag_withtag(self, newtag, tagOrId):
  1004.         self.addtag(newtag, 'withtag', tagOrId)
  1005.     def bbox(self, *args):
  1006.         return self._getints(self._do('bbox', args)) or None
  1007.     def tag_unbind(self, tagOrId, sequence):
  1008.         self.tk.call(self._w, 'bind', tagOrId, sequence, '')
  1009.     def tag_bind(self, tagOrId, sequence=None, func=None, add=None):
  1010.         return self._bind((self._w, 'bind', tagOrId),
  1011.                   sequence, func, add)
  1012.     def canvasx(self, screenx, gridspacing=None):
  1013.         return self.tk.getdouble(self.tk.call(
  1014.             self._w, 'canvasx', screenx, gridspacing))
  1015.     def canvasy(self, screeny, gridspacing=None):
  1016.         return self.tk.getdouble(self.tk.call(
  1017.             self._w, 'canvasy', screeny, gridspacing))
  1018.     def coords(self, *args):
  1019.         return map(self.tk.getdouble,
  1020.                            self.tk.splitlist(self._do('coords', args)))
  1021.     def _create(self, itemType, args, kw): # Args: (val, val, ..., cnf={})
  1022.         args = _flatten(args)
  1023.         cnf = args[-1]
  1024.         if type(cnf) in (DictionaryType, TupleType):
  1025.             args = args[:-1]
  1026.         else:
  1027.             cnf = {}
  1028.         return self.tk.getint(apply(
  1029.             self.tk.call,
  1030.             (self._w, 'create', itemType) 
  1031.             + args + self._options(cnf, kw)))
  1032.     def create_arc(self, *args, **kw):
  1033.         return self._create('arc', args, kw)
  1034.     def create_bitmap(self, *args, **kw):
  1035.         return self._create('bitmap', args, kw)
  1036.     def create_image(self, *args, **kw):
  1037.         return self._create('image', args, kw)
  1038.     def create_line(self, *args, **kw):
  1039.         return self._create('line', args, kw)
  1040.     def create_oval(self, *args, **kw):
  1041.         return self._create('oval', args, kw)
  1042.     def create_polygon(self, *args, **kw):
  1043.         return self._create('polygon', args, kw)
  1044.     def create_rectangle(self, *args, **kw):
  1045.         return self._create('rectangle', args, kw)
  1046.     def create_text(self, *args, **kw):
  1047.         return self._create('text', args, kw)
  1048.     def create_window(self, *args, **kw):
  1049.         return self._create('window', args, kw)
  1050.     def dchars(self, *args):
  1051.         self._do('dchars', args)
  1052.     def delete(self, *args):
  1053.         self._do('delete', args)
  1054.     def dtag(self, *args):
  1055.         self._do('dtag', args)
  1056.     def find(self, *args):
  1057.         return self._getints(self._do('find', args)) or ()
  1058.     def find_above(self, tagOrId):
  1059.         return self.find('above', tagOrId)
  1060.     def find_all(self):
  1061.         return self.find('all')
  1062.     def find_below(self, tagOrId):
  1063.         return self.find('below', tagOrId)
  1064.     def find_closest(self, x, y, halo=None, start=None):
  1065.         return self.find('closest', x, y, halo, start)
  1066.     def find_enclosed(self, x1, y1, x2, y2):
  1067.         return self.find('enclosed', x1, y1, x2, y2)
  1068.     def find_overlapping(self, x1, y1, x2, y2):
  1069.         return self.find('overlapping', x1, y1, x2, y2)
  1070.     def find_withtag(self, tagOrId):
  1071.         return self.find('withtag', tagOrId)
  1072.     def focus(self, *args):
  1073.         return self._do('focus', args)
  1074.     def gettags(self, *args):
  1075.         return self.tk.splitlist(self._do('gettags', args))
  1076.     def icursor(self, *args):
  1077.         self._do('icursor', args)
  1078.     def index(self, *args):
  1079.         return self.tk.getint(self._do('index', args))
  1080.     def insert(self, *args):
  1081.         self._do('insert', args)
  1082.     def itemcget(self, tagOrId, option):
  1083.         return self._do('itemcget', (tagOrId, '-'+option))
  1084.     def itemconfig(self, tagOrId, cnf=None, **kw):
  1085.         if cnf is None and not kw:
  1086.             cnf = {}
  1087.             for x in self.tk.split(
  1088.                 self._do('itemconfigure', (tagOrId))):
  1089.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1090.             return cnf
  1091.         if type(cnf) == StringType and not kw:
  1092.             x = self.tk.split(self._do('itemconfigure',
  1093.                            (tagOrId, '-'+cnf,)))
  1094.             return (x[0][1:],) + x[1:]
  1095.         self._do('itemconfigure', (tagOrId,)
  1096.              + self._options(cnf, kw))
  1097.     itemconfigure = itemconfig
  1098.     def lower(self, *args):
  1099.         self._do('lower', args)
  1100.     def move(self, *args):
  1101.         self._do('move', args)
  1102.     def postscript(self, cnf={}, **kw):
  1103.         return self._do('postscript', self._options(cnf, kw))
  1104.     def tkraise(self, *args):
  1105.         self._do('raise', args)
  1106.     lift = tkraise
  1107.     def scale(self, *args):
  1108.         self._do('scale', args)
  1109.     def scan_mark(self, x, y):
  1110.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1111.     def scan_dragto(self, x, y):
  1112.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1113.     def select_adjust(self, tagOrId, index):
  1114.         self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
  1115.     def select_clear(self):
  1116.         self.tk.call(self._w, 'select', 'clear')
  1117.     def select_from(self, tagOrId, index):
  1118.         self.tk.call(self._w, 'select', 'set', tagOrId, index)
  1119.     def select_item(self):
  1120.         self.tk.call(self._w, 'select', 'item')
  1121.     def select_to(self, tagOrId, index):
  1122.         self.tk.call(self._w, 'select', 'to', tagOrId, index)
  1123.     def type(self, tagOrId):
  1124.         return self.tk.call(self._w, 'type', tagOrId) or None
  1125.     def xview(self, *args):
  1126.         if not args:
  1127.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1128.         apply(self.tk.call, (self._w, 'xview')+args)
  1129.     def yview(self, *args):
  1130.         if not args:
  1131.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1132.         apply(self.tk.call, (self._w, 'yview')+args)
  1133.  
  1134. class Checkbutton(Widget):
  1135.     def __init__(self, master=None, cnf={}, **kw):
  1136.         Widget.__init__(self, master, 'checkbutton', cnf, kw)
  1137.     def deselect(self):
  1138.         self.tk.call(self._w, 'deselect')
  1139.     def flash(self):
  1140.         self.tk.call(self._w, 'flash')
  1141.     def invoke(self):
  1142.         self.tk.call(self._w, 'invoke')
  1143.     def select(self):
  1144.         self.tk.call(self._w, 'select')
  1145.     def toggle(self):
  1146.         self.tk.call(self._w, 'toggle')
  1147.  
  1148. class Entry(Widget):
  1149.     def __init__(self, master=None, cnf={}, **kw):
  1150.         Widget.__init__(self, master, 'entry', cnf, kw)
  1151.     def delete(self, first, last=None):
  1152.         self.tk.call(self._w, 'delete', first, last)
  1153.     def get(self):
  1154.         return self.tk.call(self._w, 'get')
  1155.     def icursor(self, index):
  1156.         self.tk.call(self._w, 'icursor', index)
  1157.     def index(self, index):
  1158.         return self.tk.getint(self.tk.call(
  1159.             self._w, 'index', index))
  1160.     def insert(self, index, string):
  1161.         self.tk.call(self._w, 'insert', index, string)
  1162.     def scan_mark(self, x):
  1163.         self.tk.call(self._w, 'scan', 'mark', x)
  1164.     def scan_dragto(self, x):
  1165.         self.tk.call(self._w, 'scan', 'dragto', x)
  1166.     def selection_adjust(self, index):
  1167.         self.tk.call(self._w, 'selection', 'adjust', index)
  1168.     select_adjust = selection_adjust
  1169.     def selection_clear(self):
  1170.         self.tk.call(self._w, 'selection', 'clear')
  1171.     select_clear = selection_clear
  1172.     def selection_from(self, index):
  1173.         self.tk.call(self._w, 'selection', 'from', index)
  1174.     select_from = selection_from
  1175.     def selection_present(self):
  1176.         return self.tk.getboolean(
  1177.             self.tk.call(self._w, 'selection', 'present'))
  1178.     select_present = selection_present
  1179.     def selection_range(self, start, end):
  1180.         self.tk.call(self._w, 'selection', 'range', start, end)
  1181.     select_range = selection_range
  1182.     def selection_to(self, index):
  1183.         self.tk.call(self._w, 'selection', 'to', index)
  1184.     select_to = selection_to
  1185.     def xview(self, index):
  1186.         self.tk.call(self._w, 'xview', index)
  1187.     def xview_moveto(self, fraction):
  1188.         self.tk.call(self._w, 'xview', 'moveto', fraction)
  1189.     def xview_scroll(self, number, what):
  1190.         self.tk.call(self._w, 'xview', 'scroll', number, what)
  1191.  
  1192. class Frame(Widget):
  1193.     def __init__(self, master=None, cnf={}, **kw):
  1194.         cnf = _cnfmerge((cnf, kw))
  1195.         extra = ()
  1196.         if cnf.has_key('class'):
  1197.             extra = ('-class', cnf['class'])
  1198.             del cnf['class']
  1199.         Widget.__init__(self, master, 'frame', cnf, {}, extra)
  1200.  
  1201. class Label(Widget):
  1202.     def __init__(self, master=None, cnf={}, **kw):
  1203.         Widget.__init__(self, master, 'label', cnf, kw)
  1204.  
  1205. class Listbox(Widget):
  1206.     def __init__(self, master=None, cnf={}, **kw):
  1207.         Widget.__init__(self, master, 'listbox', cnf, kw)
  1208.     def activate(self, index):
  1209.         self.tk.call(self._w, 'activate', index)
  1210.     def bbox(self, *args):
  1211.         return self._getints(self._do('bbox', args)) or None
  1212.     def curselection(self):
  1213.         # XXX Ought to apply self._getints()...
  1214.         return self.tk.splitlist(self.tk.call(
  1215.             self._w, 'curselection'))
  1216.     def delete(self, first, last=None):
  1217.         self.tk.call(self._w, 'delete', first, last)
  1218.     def get(self, first, last=None):
  1219.         if last:
  1220.             return self.tk.splitlist(self.tk.call(
  1221.                 self._w, 'get', first, last))
  1222.         else:
  1223.             return self.tk.call(self._w, 'get', first)
  1224.     def insert(self, index, *elements):
  1225.         apply(self.tk.call,
  1226.               (self._w, 'insert', index) + elements)
  1227.     def nearest(self, y):
  1228.         return self.tk.getint(self.tk.call(
  1229.             self._w, 'nearest', y))
  1230.     def scan_mark(self, x, y):
  1231.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1232.     def scan_dragto(self, x, y):
  1233.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1234.     def see(self, index):
  1235.         self.tk.call(self._w, 'see', index)
  1236.     def index(self, index):
  1237.         i = self.tk.call(self._w, 'index', index)
  1238.         if i == 'none': return None
  1239.         return self.tk.getint(i)
  1240.     def select_adjust(self, index):
  1241.         self.tk.call(self._w, 'select', 'adjust', index)
  1242.     def select_anchor(self, index):
  1243.         self.tk.call(self._w, 'selection', 'anchor', index)
  1244.     def select_clear(self, first, last=None):
  1245.         self.tk.call(self._w,
  1246.                  'selection', 'clear', first, last)
  1247.     def select_includes(self, index):
  1248.         return self.tk.getboolean(self.tk.call(
  1249.             self._w, 'selection', 'includes', index))
  1250.     def select_set(self, first, last=None):
  1251.         self.tk.call(self._w, 'selection', 'set', first, last)
  1252.     def size(self):
  1253.         return self.tk.getint(self.tk.call(self._w, 'size'))
  1254.     def xview(self, *what):
  1255.         if not what:
  1256.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1257.         apply(self.tk.call, (self._w, 'xview')+what)
  1258.     def yview(self, *what):
  1259.         if not what:
  1260.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1261.         apply(self.tk.call, (self._w, 'yview')+what)
  1262.  
  1263. class Menu(Widget):
  1264.     def __init__(self, master=None, cnf={}, **kw):
  1265.         Widget.__init__(self, master, 'menu', cnf, kw)
  1266.     def tk_bindForTraversal(self):
  1267.         pass # obsolete since Tk 4.0
  1268.     def tk_mbPost(self):
  1269.         self.tk.call('tk_mbPost', self._w)
  1270.     def tk_mbUnpost(self):
  1271.         self.tk.call('tk_mbUnpost')
  1272.     def tk_traverseToMenu(self, char):
  1273.         self.tk.call('tk_traverseToMenu', self._w, char)
  1274.     def tk_traverseWithinMenu(self, char):
  1275.         self.tk.call('tk_traverseWithinMenu', self._w, char)
  1276.     def tk_getMenuButtons(self):
  1277.         return self.tk.call('tk_getMenuButtons', self._w)
  1278.     def tk_nextMenu(self, count):
  1279.         self.tk.call('tk_nextMenu', count)
  1280.     def tk_nextMenuEntry(self, count):
  1281.         self.tk.call('tk_nextMenuEntry', count)
  1282.     def tk_invokeMenu(self):
  1283.         self.tk.call('tk_invokeMenu', self._w)
  1284.     def tk_firstMenu(self):
  1285.         self.tk.call('tk_firstMenu', self._w)
  1286.     def tk_mbButtonDown(self):
  1287.         self.tk.call('tk_mbButtonDown', self._w)
  1288.     def tk_popup(self, x, y, entry=""):
  1289.         self.tk.call('tk_popup', self._w, x, y, entry)
  1290.     def activate(self, index):
  1291.         self.tk.call(self._w, 'activate', index)
  1292.     def add(self, itemType, cnf={}, **kw):
  1293.         apply(self.tk.call, (self._w, 'add', itemType) 
  1294.               + self._options(cnf, kw))
  1295.     def add_cascade(self, cnf={}, **kw):
  1296.         self.add('cascade', cnf or kw)
  1297.     def add_checkbutton(self, cnf={}, **kw):
  1298.         self.add('checkbutton', cnf or kw)
  1299.     def add_command(self, cnf={}, **kw):
  1300.         self.add('command', cnf or kw)
  1301.     def add_radiobutton(self, cnf={}, **kw):
  1302.         self.add('radiobutton', cnf or kw)
  1303.     def add_separator(self, cnf={}, **kw):
  1304.         self.add('separator', cnf or kw)
  1305.     def insert(self, index, itemType, cnf={}, **kw):
  1306.         apply(self.tk.call, (self._w, 'insert', index, itemType) 
  1307.               + self._options(cnf, kw))
  1308.     def insert_cascade(self, index, cnf={}, **kw):
  1309.         self.insert(index, 'cascade', cnf or kw)
  1310.     def insert_checkbutton(self, index, cnf={}, **kw):
  1311.         self.insert(index, 'checkbutton', cnf or kw)
  1312.     def insert_command(self, index, cnf={}, **kw):
  1313.         self.insert(index, 'command', cnf or kw)
  1314.     def insert_radiobutton(self, index, cnf={}, **kw):
  1315.         self.insert(index, 'radiobutton', cnf or kw)
  1316.     def insert_separator(self, index, cnf={}, **kw):
  1317.         self.insert(index, 'separator', cnf or kw)
  1318.     def delete(self, index1, index2=None):
  1319.         self.tk.call(self._w, 'delete', index1, index2)
  1320.     def entryconfig(self, index, cnf=None, **kw):
  1321.         if cnf is None and not kw:
  1322.             cnf = {}
  1323.             for x in self.tk.split(apply(self.tk.call,
  1324.                 (self._w, 'entryconfigure', index))):
  1325.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  1326.             return cnf
  1327.         if type(cnf) == StringType and not kw:
  1328.             x = self.tk.split(apply(self.tk.call,
  1329.                 (self._w, 'entryconfigure', index, '-'+cnf)))
  1330.             return (x[0][1:],) + x[1:]
  1331.         apply(self.tk.call, (self._w, 'entryconfigure', index)
  1332.               + self._options(cnf, kw))
  1333.     entryconfigure = entryconfig
  1334.     def index(self, index):
  1335.         i = self.tk.call(self._w, 'index', index)
  1336.         if i == 'none': return None
  1337.         return self.tk.getint(i)
  1338.     def invoke(self, index):
  1339.         return self.tk.call(self._w, 'invoke', index)
  1340.     def post(self, x, y):
  1341.         self.tk.call(self._w, 'post', x, y)
  1342.     def unpost(self):
  1343.         self.tk.call(self._w, 'unpost')
  1344.     def yposition(self, index):
  1345.         return self.tk.getint(self.tk.call(
  1346.             self._w, 'yposition', index))
  1347.  
  1348. class Menubutton(Widget):
  1349.     def __init__(self, master=None, cnf={}, **kw):
  1350.         Widget.__init__(self, master, 'menubutton', cnf, kw)
  1351.  
  1352. class Message(Widget):
  1353.     def __init__(self, master=None, cnf={}, **kw):
  1354.         Widget.__init__(self, master, 'message', cnf, kw)
  1355.  
  1356. class Radiobutton(Widget):
  1357.     def __init__(self, master=None, cnf={}, **kw):
  1358.         Widget.__init__(self, master, 'radiobutton', cnf, kw)
  1359.     def deselect(self):
  1360.         self.tk.call(self._w, 'deselect')
  1361.     def flash(self):
  1362.         self.tk.call(self._w, 'flash')
  1363.     def invoke(self):
  1364.         self.tk.call(self._w, 'invoke')
  1365.     def select(self):
  1366.         self.tk.call(self._w, 'select')
  1367.  
  1368. class Scale(Widget):
  1369.     def __init__(self, master=None, cnf={}, **kw):
  1370.         Widget.__init__(self, master, 'scale', cnf, kw)
  1371.     def get(self):
  1372.         value = self.tk.call(self._w, 'get')
  1373.         try:
  1374.             return self.tk.getint(value)
  1375.         except TclError:
  1376.             return self.tk.getdouble(value)
  1377.     def set(self, value):
  1378.         self.tk.call(self._w, 'set', value)
  1379.  
  1380. class Scrollbar(Widget):
  1381.     def __init__(self, master=None, cnf={}, **kw):
  1382.         Widget.__init__(self, master, 'scrollbar', cnf, kw)
  1383.     def activate(self, index):
  1384.         self.tk.call(self._w, 'activate', index)
  1385.     def delta(self, deltax, deltay):
  1386.         return self.getdouble(self.tk.call(
  1387.             self._w, 'delta', deltax, deltay))
  1388.     def fraction(self, x, y):
  1389.         return self.getdouble(self.tk.call(
  1390.             self._w, 'fraction', x, y))
  1391.     def identify(self, x, y):
  1392.         return self.tk.call(self._w, 'identify', x, y)
  1393.     def get(self):
  1394.         return self._getdoubles(self.tk.call(self._w, 'get'))
  1395.     def set(self, *args):
  1396.         apply(self.tk.call, (self._w, 'set')+args)
  1397.  
  1398. class Text(Widget):
  1399.     def __init__(self, master=None, cnf={}, **kw):
  1400.         Widget.__init__(self, master, 'text', cnf, kw)
  1401.     def bbox(self, *args):
  1402.         return self._getints(self._do('bbox', args)) or None
  1403.     def tk_textSelectTo(self, index):
  1404.         self.tk.call('tk_textSelectTo', self._w, index)
  1405.     def tk_textBackspace(self):
  1406.         self.tk.call('tk_textBackspace', self._w)
  1407.     def tk_textIndexCloser(self, a, b, c):
  1408.         self.tk.call('tk_textIndexCloser', self._w, a, b, c)
  1409.     def tk_textResetAnchor(self, index):
  1410.         self.tk.call('tk_textResetAnchor', self._w, index)
  1411.     def compare(self, index1, op, index2):
  1412.         return self.tk.getboolean(self.tk.call(
  1413.             self._w, 'compare', index1, op, index2))
  1414.     def debug(self, boolean=None):
  1415.         return self.tk.getboolean(self.tk.call(
  1416.             self._w, 'debug', boolean))
  1417.     def delete(self, index1, index2=None):
  1418.         self.tk.call(self._w, 'delete', index1, index2)
  1419.     def dlineinfo(self, index):
  1420.         return self._getints(self.tk.call(self._w, 'dlineinfo', index))
  1421.     def get(self, index1, index2=None):
  1422.         return self.tk.call(self._w, 'get', index1, index2)
  1423.     def index(self, index):
  1424.         return self.tk.call(self._w, 'index', index)
  1425.     def insert(self, index, chars, *args):
  1426.         apply(self.tk.call, (self._w, 'insert', index, chars)+args)
  1427.     def mark_gravity(self, markName, direction=None):
  1428.         return apply(self.tk.call,
  1429.                  (self._w, 'mark', 'gravity', markName, direction))
  1430.     def mark_names(self):
  1431.         return self.tk.splitlist(self.tk.call(
  1432.             self._w, 'mark', 'names'))
  1433.     def mark_set(self, markName, index):
  1434.         self.tk.call(self._w, 'mark', 'set', markName, index)
  1435.     def mark_unset(self, *markNames):
  1436.         apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
  1437.     def scan_mark(self, x, y):
  1438.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1439.     def scan_dragto(self, x, y):
  1440.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1441.     def search(self, pattern, index, stopindex=None,
  1442.            forwards=None, backwards=None, exact=None,
  1443.            regexp=None, nocase=None, count=None):
  1444.         args = [self._w, 'search']
  1445.         if forwards: args.append('-forwards')
  1446.         if backwards: args.append('-backwards')
  1447.         if exact: args.append('-exact')
  1448.         if regexp: args.append('-regexp')
  1449.         if nocase: args.append('-nocase')
  1450.         if count: args.append('-count'); args.append(count)
  1451.         if pattern[0] == '-': args.append('--')
  1452.         args.append(pattern)
  1453.         args.append(index)
  1454.         if stopindex: args.append(stopindex)
  1455.         return apply(self.tk.call, tuple(args))
  1456.     def see(self, index):
  1457.         self.tk.call(self._w, 'see', index)
  1458.     def tag_add(self, tagName, index1, index2=None):
  1459.         self.tk.call(
  1460.             self._w, 'tag', 'add', tagName, index1, index2)
  1461.     def tag_unbind(self, tagName, sequence):
  1462.         self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
  1463.     def tag_bind(self, tagName, sequence, func, add=None):
  1464.         return self._bind((self._w, 'tag', 'bind', tagName),
  1465.                   sequence, func, add)
  1466.     def tag_cget(self, tagName, option):
  1467.         return self.tk.call(self._w, 'tag', 'cget', tagName, option)
  1468.     def tag_config(self, tagName, cnf={}, **kw):
  1469.         if type(cnf) == StringType:
  1470.             x = self.tk.split(self.tk.call(
  1471.                 self._w, 'tag', 'configure', tagName, '-'+cnf))
  1472.             return (x[0][1:],) + x[1:]
  1473.         apply(self.tk.call, 
  1474.               (self._w, 'tag', 'configure', tagName)
  1475.               + self._options(cnf, kw))
  1476.     tag_configure = tag_config
  1477.     def tag_delete(self, *tagNames):
  1478.         apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
  1479.     def tag_lower(self, tagName, belowThis=None):
  1480.         self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
  1481.     def tag_names(self, index=None):
  1482.         return self.tk.splitlist(
  1483.             self.tk.call(self._w, 'tag', 'names', index))
  1484.     def tag_nextrange(self, tagName, index1, index2=None):
  1485.         return self.tk.splitlist(self.tk.call(
  1486.             self._w, 'tag', 'nextrange', tagName, index1, index2))
  1487.     def tag_raise(self, tagName, aboveThis=None):
  1488.         self.tk.call(
  1489.             self._w, 'tag', 'raise', tagName, aboveThis)
  1490.     def tag_ranges(self, tagName):
  1491.         return self.tk.splitlist(self.tk.call(
  1492.             self._w, 'tag', 'ranges', tagName))
  1493.     def tag_remove(self, tagName, index1, index2=None):
  1494.         self.tk.call(
  1495.             self._w, 'tag', 'remove', tagName, index1, index2)
  1496.     def window_cget(self, index, option):
  1497.         return self.tk.call(self._w, 'window', 'cget', index, option)
  1498.     def window_config(self, index, cnf={}, **kw):
  1499.         if type(cnf) == StringType:
  1500.             x = self.tk.split(self.tk.call(
  1501.                 self._w, 'window', 'configure',
  1502.                 index, '-'+cnf))
  1503.             return (x[0][1:],) + x[1:]
  1504.         apply(self.tk.call, 
  1505.               (self._w, 'window', 'configure', index)
  1506.               + self._options(cnf, kw))
  1507.     window_configure = window_config
  1508.     def window_create(self, index, cnf={}, **kw):
  1509.         apply(self.tk.call, 
  1510.               (self._w, 'window', 'create', index)
  1511.               + self._options(cnf, kw))
  1512.     def window_names(self):
  1513.         return self.tk.splitlist(
  1514.             self.tk.call(self._w, 'window', 'names'))
  1515.     def xview(self, *what):
  1516.         if not what:
  1517.             return self._getdoubles(self.tk.call(self._w, 'xview'))
  1518.         apply(self.tk.call, (self._w, 'xview')+what)
  1519.     def yview(self, *what):
  1520.         if not what:
  1521.             return self._getdoubles(self.tk.call(self._w, 'yview'))
  1522.         apply(self.tk.call, (self._w, 'yview')+what)
  1523.     def yview_pickplace(self, *what):
  1524.         apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
  1525.  
  1526. class _setit:
  1527.     def __init__(self, var, value):
  1528.         self.__value = value
  1529.         self.__var = var
  1530.  
  1531.     def __call__(self, *args):
  1532.         self.__var.set(self.__value)
  1533.  
  1534. class OptionMenu(Menubutton):
  1535.     def __init__(self, master, variable, value, *values):
  1536.         kw = {"borderwidth": 2, "textvariable": variable,
  1537.               "indicatoron": 1, "relief": RAISED, "anchor": "c",
  1538.               "highlightthickness": 2}
  1539.         Widget.__init__(self, master, "menubutton", kw)
  1540.         self.widgetName = 'tk_optionMenu'
  1541.         menu = self.__menu = Menu(self, name="menu", tearoff=0)
  1542.         self.menuname = menu._w
  1543.         menu.add_command(label=value, command=_setit(variable, value))
  1544.         for v in values:
  1545.             menu.add_command(label=v, command=_setit(variable, v))
  1546.         self["menu"] = menu
  1547.  
  1548.     def __getitem__(self, name):
  1549.         if name == 'menu':
  1550.             return self.__menu
  1551.         return Widget.__getitem__(self, name)
  1552.  
  1553.     def destroy(self):
  1554.         Menubutton.destroy(self)
  1555.         self.__menu = None
  1556.  
  1557. class Image:
  1558.     def __init__(self, imgtype, name=None, cnf={}, **kw):
  1559.         self.name = None
  1560.         master = _default_root
  1561.         if not master: raise RuntimeError, 'Too early to create image'
  1562.         self.tk = master.tk
  1563.         if not name: name = `id(self)`
  1564.         if kw and cnf: cnf = _cnfmerge((cnf, kw))
  1565.         elif kw: cnf = kw
  1566.         options = ()
  1567.         for k, v in cnf.items():
  1568.             if callable(v):
  1569.                 v = self._register(v)
  1570.             options = options + ('-'+k, v)
  1571.         apply(self.tk.call,
  1572.               ('image', 'create', imgtype, name,) + options)
  1573.         self.name = name
  1574.     def __str__(self): return self.name
  1575.     def __del__(self):
  1576.         if self.name:
  1577.             self.tk.call('image', 'delete', self.name)
  1578.     def __setitem__(self, key, value):
  1579.         self.tk.call(self.name, 'configure', '-'+key, value)
  1580.     def __getitem__(self, key):
  1581.         return self.tk.call(self.name, 'configure', '-'+key)
  1582.     def height(self):
  1583.         return self.tk.getint(
  1584.             self.tk.call('image', 'height', self.name))
  1585.     def type(self):
  1586.         return self.tk.call('image', 'type', self.name)
  1587.     def width(self):
  1588.         return self.tk.getint(
  1589.             self.tk.call('image', 'width', self.name))
  1590.  
  1591. class PhotoImage(Image):
  1592.     def __init__(self, name=None, cnf={}, **kw):
  1593.         apply(Image.__init__, (self, 'photo', name, cnf), kw)
  1594.     def blank(self):
  1595.         self.tk.call(self.name, 'blank')
  1596.     def cget(self, option):
  1597.         return self.tk.call(self.name, 'cget', '-' + option)
  1598.     # XXX config
  1599.     def __getitem__(self, key):
  1600.         return self.tk.call(self.name, 'cget', '-' + key)
  1601.     def copy(self):
  1602.         destImage = PhotoImage()
  1603.         self.tk.call(destImage, 'copy', self.name)
  1604.         return destImage
  1605.     def zoom(self,x,y=''):
  1606.         destImage = PhotoImage()
  1607.         if y=='': y=x
  1608.         self.tk.call(destImage, 'copy', self.name, '-zoom',x,y)
  1609.         return destImage
  1610.     def subsample(self,x,y=''):
  1611.         destImage = PhotoImage()
  1612.         if y=='': y=x
  1613.         self.tk.call(destImage, 'copy', self.name, '-subsample',x,y)
  1614.         return destImage
  1615.     def get(self, x, y):
  1616.         return self.tk.call(self.name, 'get', x, y)
  1617.     def put(self, data, to=None):
  1618.         args = (self.name, 'put', data)
  1619.         if to:
  1620.             args = args + to
  1621.         apply(self.tk.call, args)
  1622.     # XXX read
  1623.     def write(self, filename, format=None, from_coords=None):
  1624.         args = (self.name, 'write', filename)
  1625.         if format:
  1626.             args = args + ('-format', format)
  1627.         if from_coords:
  1628.             args = args + ('-from',) + tuple(from_coords)
  1629.         apply(self.tk.call, args)
  1630.  
  1631. class BitmapImage(Image):
  1632.     def __init__(self, name=None, cnf={}, **kw):
  1633.         apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
  1634.  
  1635. def image_names(): return _default_root.tk.call('image', 'names')
  1636. def image_types(): return _default_root.tk.call('image', 'types')
  1637.  
  1638. ######################################################################
  1639. # Extensions:
  1640.  
  1641. class Studbutton(Button):
  1642.     def __init__(self, master=None, cnf={}, **kw):
  1643.         Widget.__init__(self, master, 'studbutton', cnf, kw)
  1644.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1645.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1646.         self.bind('<1>',               self.tkButtonDown)
  1647.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1648.  
  1649. class Tributton(Button):
  1650.     def __init__(self, master=None, cnf={}, **kw):
  1651.         Widget.__init__(self, master, 'tributton', cnf, kw)
  1652.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1653.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1654.         self.bind('<1>',               self.tkButtonDown)
  1655.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1656.         self['fg']               = self['bg']
  1657.         self['activebackground'] = self['bg']
  1658.  
  1659. ######################################################################
  1660. # Test:
  1661.  
  1662. def _test():
  1663.     root = Tk()
  1664.     label = Label(root, text="Proof-of-existence test for Tk")
  1665.     label.pack()
  1666.     test = Button(root, text="Click me!",
  1667.               command=lambda root=root: root.test.config(
  1668.                   text="[%s]" % root.test['text']))
  1669.     test.pack()
  1670.     root.test = test
  1671.     quit = Button(root, text="QUIT", command=root.destroy)
  1672.     quit.pack()
  1673.     root.mainloop()
  1674.  
  1675. if __name__ == '__main__':
  1676.     _test()
  1677.  
  1678.  
  1679. # Emacs cruft
  1680. # Local Variables:
  1681. # py-indent-offset: 8
  1682. # End:
  1683.